package com.qf.flashlib.adapter;

import android.support.v4.util.SparseArrayCompat;

/**
 * 2019/4/25 11:19
 */
class AdapterHelper<T> {

    private SparseArrayCompat<RViewItem<T>> rViewItems;


    AdapterHelper() {
        rViewItems = new SparseArrayCompat<>();
    }


    void addItemViewType(RViewItem<T> rViewItem) {
        if (rViewItem != null) {
            rViewItems.put(rViewItems.size(), rViewItem);
        }
    }

    //获取viewType的数量
    int getItemViewTypeCount() {
        return rViewItems.size();
    }

    RViewItem<T> getRView(int viewType) {
        return rViewItems.get(viewType);
    }

    void convert(RViewHolder holder, T entity, int position, int itemSize) {
        for (int i = 0; i < rViewItems.size(); i++) {
            RViewItem<T> rViewItem = rViewItems.valueAt(i);
            if (rViewItem.isItemView(entity, position)) {
                rViewItem.convert(holder, entity, position, itemSize);
                return;
            }

        }
    }

    int getItemViewType(T entity, int position) {
        for (int i = rViewItems.size() - 1; i >= 0; i--) {
            RViewItem<T> rViewItem = rViewItems.valueAt(i);
            if (rViewItem.isItemView(entity, position)) {
                return rViewItems.keyAt(i);
            }

        }
        throw new IllegalArgumentException("该位置没有匹配的条目样式");
    }
}
